﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BattleTipsCtrl : MonoBehaviour {

    public UnityEngine.GameObject battleTipsPref;

    private void Start()
    {
        battleTipsPref.SetActive(false);
    }
    //Dictionary<string, BottomTips> bottomTips = new Dictionary<string, BottomTips>();
    public ToastTips ShowTips(string tipsId, int priority, string content)
    {
        if (!this)
            return null;

        Dictionary<string, UnityEngine.GameObject> bottomTips = widgetCache.GetDict<ToastTips>();

        foreach (var exist in bottomTips)
        {

            if (exist.Value != null
                && exist.Key != tipsId
                && exist.Value.GetComponent<ToastTips>().Priority > priority
                && exist.Value.GetComponent<ToastTips>().isShowing)
            {
                //已显示优先度高的则返回
                return null; 
            }
        }
        //清空其他
        foreach (var exist in bottomTips)
        {
            if (exist.Key != tipsId)
            {
                HideTips(exist.Key);
            }
        }
        
        bool newObj;
        var cache = widgetCache.GetCache<ToastTips>(battleTipsPref, tipsId, out newObj);
        cache.transform.SetParent(transform);
        cache.transform.localPosition = Vector3.zero;
        //cache.GetComponent<RectTransform>().SetUIPosition( battleTipsPref.transform.position);

        var bt = cache.GetComponent<ToastTips>();
        if (!bt.isShowing)
        {
            bt.gameObject.SetActive(true);
            bt.Show();
            bt.Priority = priority;
            bt.SetContent(content);
        }
        return bt;
    }

    public void HideTips(string tipsId)
    {
        Dictionary<string, UnityEngine.GameObject> bottomTips = widgetCache.GetDict<ToastTips>();
        if (bottomTips.ContainsKey(tipsId))
        {
            if (bottomTips[tipsId] && bottomTips[tipsId].GetComponent<ToastTips>().isShowing)
                bottomTips[tipsId].GetComponent<ToastTips>().Hide();
        }
    }

    WidgetCache m_WidgetCache;
    WidgetCache widgetCache
    {
        get
        {
            if (m_WidgetCache == null) m_WidgetCache = GetComponent<WidgetCache>();
            if (m_WidgetCache == null) m_WidgetCache = gameObject.AddComponent<WidgetCache>();

            return m_WidgetCache;
        }
    }
}
